﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BidFunLib.Entity;
using BidFunLib.Services;

namespace BidFunLib.Entity
{
    /// <summary>
    /// Contient toutes les informations necessaires de manière 'dynamique' ;o)
    /// </summary>
    public class StatsBidSaleLive
    {
        /// <summary>
        /// 
        /// </summary>
        public StatsBidObjectGlobal statsBidObjectGlobal { get; set; }

        /// <summary>
        /// Contient les statistiques des utilisateurs qui ont pariés sur la vente.
        /// </summary>
        private Dictionary<String, StatsBidSaleLiveUser> StatsBidSaleLiveUserMap = null;



        public StatsBidSaleLiveUser getStatsBidSaleLiveUserBypseudo(String pseudo)
        {
            return StatsBidSaleLiveUserMap[pseudo];
        }

        /// <summary>
        /// Retourne le nombre de joueur dans la vente depuis qu'on la trace 
        /// (pas forcement depuis le debut).
        /// </summary>
        public Int32 NbUserInSale
        {
            get
            {
                return StatsBidSaleLiveUserMap.Count;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deltaTimeInHeureMinuteSeconds"></param>
        /// <returns></returns>
        public Int32 getNbUSerStillActiveInSale(String deltaTimeInHeureMinuteSeconds)
        {
            // premièrement nous devons raffraichir tous les delta time de tous les joueurs
            foreach(StatsBidSaleLiveUser statsBidSaleLiveUser in StatsBidSaleLiveUserMap.Values)
            {
                statsBidSaleLiveUser.updateNoBidDurationTime();
            }

            return 0;
        }




        /// <summary>
        /// 
        /// </summary>
        /// <param name="?"></param>
        /// <param name="?"></param>
        public void updateStatsBidSaleLiveWithBidLogger(BidSale bidSale, BidLogger bidLogger) 
        {
            StatsBidSaleLiveUser statsBidSaleLiveUser = getStatsBidSaleLiveUser(bidSale, bidLogger);

            // delegate updating statistics 
            statsBidSaleLiveUser.updateStatsBidSaleLiveWithBidLogger(bidSale, bidLogger);

            // update all user for last bid time
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bidSale"></param>
        /// <param name="bidLogger"></param>
        /// <returns></returns>
        private StatsBidSaleLiveUser getStatsBidSaleLiveUser(BidSale bidSale, BidLogger bidLogger)
        {
            StatsBidSaleLiveUser statsBidSaleLiveUser = null;

            // search for UserBidSaleDynamicData in dictionnary if doesn't exist we create it.
            if (!StatsBidSaleLiveUserMap.ContainsKey(bidLogger.BidUser.Pseudo))
            {
                // we create new object : constructor will retreive all needed statistics data from DB.
                // this constructor call will retreive all object statistics    
                statsBidSaleLiveUser = new StatsBidSaleLiveUser(bidSale, bidLogger);

                // we add it to the Dictionary
                StatsBidSaleLiveUserMap.Add(bidLogger.BidUser.Pseudo, statsBidSaleLiveUser);
            }
            else
            {
                // we retreive the object in dictionary
                statsBidSaleLiveUser = StatsBidSaleLiveUserMap[bidLogger.BidUser.Pseudo];
            }

            return statsBidSaleLiveUser;
        }

        /// <summary>
        /// update les statistiques dynamiques de la bidSale pour un nouveau et un seul BidLogger
        /// </summary>
        /// <param name="bidSale"></param>
        public StatsBidSaleLive(BidSale bidSale, BidLogger bidLogger)
        {
            // we must retreive statistics of the users for this object
            StatsBidSaleLiveUserMap = new Dictionary<string, StatsBidSaleLiveUser>();
            statsBidObjectGlobal = StatsBidObjectGlobalManager.Instance.getStatsBidObjectGlobalByBidObject(bidSale.BidObject);
        }

    }
}
